package encome.work.utils;

import encome.work.dao.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * 实现FastDFS文件管理功能
 *  文件上传
 *  文件删除
 *  文件下载
 *  文件信息获取
 *  storage信息获取
 *  Tracker信息获取
 */
public class FastDFSUtil {

    /**
     * 加载Tracker链接信息
     */
    static {
        try {
            //查找classpath下的文件路径
            String path = new ClassPathResource("fdfs_client.conf").getPath();
            ClientGlobal.init(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件上传
     * @param fastDFSFile 上传的文件信息封装
     */
    public static String[] upload(FastDFSFile fastDFSFile) throws Exception{
        //附加参数
        NameValuePair[] meta_list = new NameValuePair[1];
        meta_list[0] = new NameValuePair("author", fastDFSFile.getAuthor());

        String[] uploads = null;

        try {

            //获取TrackerServer
            TrackerServer trackerServer = getTrackerServer();

            //通过TrackerClient链接信息可以获得Storage的链接信息，创建StorageClient对象存储Storage的链接信息
            StorageClient storageClient = new StorageClient(trackerServer, null);

            /**
             * 通过StorageClient访问Storage，实现文件上传
             * 1.上传文件的字节组数
             * 2.文件的扩展名
             * 3.附加参数  如：拍摄地址：SH
             */
            uploads = storageClient.upload_file(fastDFSFile.getContent(), fastDFSFile.getExt(), meta_list);

        }catch (Exception e){
            e.printStackTrace();
        }
        return uploads;
    }

    /**
     * 获取文件信息
     * @param groupName 文件的组名
     * @param remoteFileName 文件的存储路径名
     */
    public static FileInfo getFile(String groupName, String remoteFileName)throws Exception{

        try {
            //获取TrackerServer
            TrackerServer trackerServer = getTrackerServer();

            //获取Storage信息，创建StorageClient
            StorageClient storageClient = new StorageClient(trackerServer, null);

            //获取文件信息
            return storageClient.get_file_info(groupName, remoteFileName);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 下载文件
     * @param groupName 文件的组名
     * @param remoteFileName 文件的存储路径名
     */
    public static InputStream downloadFile(String groupName, String remoteFileName) throws Exception{

        try {
            //获取TrackerServer
            TrackerServer trackerServer = getTrackerServer();

            //获取Storage信息，创建StorageClient
            StorageClient storageClient = new StorageClient(trackerServer, null);

            byte[] buffer = storageClient.download_file(groupName, remoteFileName);
            return new ByteArrayInputStream(buffer);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除文件
     * @param groupName 文件的组名
     * @param remoteFileName 文件的存储路径名
     */
    public static void deleteFile(String groupName, String remoteFileName) throws Exception{
        try {
            //获取TrackerServer
            TrackerServer trackerServer = getTrackerServer();

            //获取Storage信息，创建StorageClient
            StorageClient storageClient = new StorageClient(trackerServer, null);

            storageClient.delete_file(groupName, remoteFileName);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /**
     * 获取storage信息
     */
    public static StorageServer getStorages() throws Exception{
        try {
            TrackerClient trackerClient = new TrackerClient();

            TrackerServer trackerServer = trackerClient.getConnection();

            //获取Storage信息
            return trackerClient.getStoreStorage(trackerServer);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 获取storage的IP和端口信息
     */
    public static ServerInfo[] getServerInfo(String groupName, String remoteFileName) throws Exception{

        try {
            TrackerClient trackerClient = new TrackerClient();

            TrackerServer trackerServer = trackerClient.getConnection();

            //获取Storage的IP和端口信息
            return trackerClient.getFetchStorages(trackerServer,groupName, remoteFileName);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取tracker信息
     */
    public static String getTrackerInfo()throws Exception{

        try {
            //获取TrackerServer
            TrackerServer trackerServer = getTrackerServer();

            //Tracker的IP，http端口
            String ip = trackerServer.getInetSocketAddress().getHostString();
            int tracker_http_port = ClientGlobal.getG_tracker_http_port();//8080

            return "http://" + ip + ":" + tracker_http_port;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取tracker
     */
    public static TrackerServer getTrackerServer() throws Exception{
        //创建一个TrackerClient对象，通过TrackerClient对象访问TrackerServer
        TrackerClient trackerClient = new TrackerClient();

        //通过TrackerClient获取TrackerServer的链接对象
        return trackerClient.getConnection();
    }

//    public static void main(String[] args) throws Exception {
//        deleteFile("group1","M00/00/00/wKiYgGOW5KmARHwtAACaH5YR03o556.PNG");
//    }
}
